/*
 * BanListDAO.java
 *
 * Created on June 21, 2007, 8:04 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package jhub.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import jhub.management.Ban;

/**
 *
 * @author dbotelho
 */
public class BanListDAO extends AbstractDAO{
    
    public enum BanType{ kick , ban };
    private final PreparedStatement addBanStatement;
    private final PreparedStatement idIpBannedStatement;
        
    /** Creates a new instance of BanListDAO */
    public BanListDAO(Connection conn) throws SQLException {
        super(conn);
        this.addBanStatement = conn.prepareStatement("INSERT INTO \"banlist\" (nick,email,share,ip,ban_date,ban_till,reason,ban_type) VALUES(?,?,?,?,?,?,?,?)");
        this.idIpBannedStatement = conn.prepareStatement("SELECT * FROM \"banlist\" WHERE ip = ?");
    
    }
    
    public boolean isIPBanned(String ip) throws SQLException{
        String sql = "SELECT * FROM \"banlist\" WHERE ";
        /*sql += (nick!=null)? "nick LIKE '"+nick+"'":"";
        sql += (email!=null)? "email LIKE '"+email+"'":"";
        sql += (share!=null)? "share LIKE '"+share+"'":"";*/
        sql += (ip!=null)? "ip LIKE '"+ip+"'":"";
        sql += " AND ban_till > "+new Date().getTime();
        
        ResultSet rs = conn.createStatement().executeQuery(sql);
        return rs.next();
    }
    
    public void listBanned(String nick,String email,Long share,String ip) throws SQLException{
        String sql = "SELECT * FROM \"banlist\" WHERE ";
        sql += (nick!=null)? "nick LIKE '"+nick+"'":"";
        sql += (email!=null)? "email LIKE '"+email+"'":"";
        sql += (share!=null)? "share LIKE '"+share+"'":"";
        sql += (ip!=null)? "ip LIKE '"+ip+"'":"";
        
        ResultSet rs = conn.createStatement().executeQuery(sql);
        while(rs.next()){
            System.out.println(rs.getString("nick"));
        }
    }
    
    private boolean isBanned(String nick,String email,String share,String ip) throws SQLException{
        String sql = "SELECT * FROM \"banlist\" WHERE ";
        sql += (nick!=null)? "nick LIKE '"+nick+"'":"";
        sql += (email!=null)? "email LIKE '"+email+"'":"";
        sql += (share!=null)? "share LIKE '"+share+"'":"'";
        sql += (ip!=null)? "ip LIKE '"+ip+"'":"";
        
        ResultSet rs = conn.createStatement().executeQuery(sql);
        
        return rs.next();
    }

    
    public boolean banIP(BanType ban_type,String ip,String reason, Date ban_date, Date ban_till) throws SQLException{        
        return addBan(null,null,null,ip,ban_date,ban_till,reason,ban_type);
    }
    
    public boolean banShare(BanType ban_type,String share,String reason, Date ban_date, Date ban_till) throws SQLException{        
        return addBan(null,null,share,null,ban_date,ban_till,reason,ban_type);
    }
    
    public boolean banEmail(BanType ban_type,String email,String reason, Date ban_date, Date ban_till) throws SQLException{        
        return addBan(null,email,null,null,ban_date,ban_till,reason,ban_type);
    }
    
    public boolean banNick(BanType ban_type,String nick,String reason, Date ban_date, Date ban_till) throws SQLException{        
        return addBan(nick,null,null,null,ban_date,ban_till,reason,ban_type);
    }
    
    private boolean addBan(String nick,String email,String share,String ip,Date ban_date,Date ban_till, String reason,BanType ban_type) throws SQLException{
        if(ban_date==null) ban_date = new Date();
        
        addBanStatement.setString(1,(nick!=null)? nick:null);
        addBanStatement.setString(2,(email!=null)? email:null);
        addBanStatement.setString(3,(share!=null)? share:null);
        addBanStatement.setString(4,(ip!=null)? ip:null);
        addBanStatement.setLong(5,ban_date.getTime() );
        if(ban_till==null)
            addBanStatement.setNull(6,java.sql.Types.NULL);
        else
            addBanStatement.setLong(6,(ban_till!=null)? ban_till.getTime():null );
        //addBanStatement.setDate(6, (ban_till!=null)? (java.sql.Date) ban_till:null);
        addBanStatement.setString(7, (reason!=null)? reason:null);
        addBanStatement.setString(8,(ban_type!=null)? ban_type.toString():null);
        
        addBanStatement.executeUpdate();
        
        return true;
    }

    public void save(Ban ban) throws SQLException {
        
        String nick = ban.getNick();
        String email = ban.getEmail();
        String share = ban.getShare();
        String ip = ban.getIp();
        Date ban_date = ban.getBanDate();
        Date ban_till = ban.getBanTill();
        String reason = ban.getReason();
        String ban_type = "kick";
        
        addBanStatement.setString(1,(nick!=null)? nick:null);
        addBanStatement.setString(2,(email!=null)? email:null);
        addBanStatement.setString(3,(share!=null)? share:null);
        addBanStatement.setString(4,(ip!=null)? ip:null);
        addBanStatement.setLong(5,ban_date.getTime() );
        if(ban_till==null)
            addBanStatement.setNull(6,java.sql.Types.NULL);
        else
            addBanStatement.setLong(6,(ban_till!=null)? ban_till.getTime():null );
        addBanStatement.setString(7, (reason!=null)? reason:null);
        addBanStatement.setString(8,(ban_type!=null)? ban_type.toString():null);
        
        addBanStatement.executeUpdate();
    }
    
}
